package com.starblink.search.result.ui.fragment.store

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.starblink.android.basic.ext.string
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.common.view.store.SearchStoreRecommendModel
import com.starblink.basic.apollo.ext.optional
import com.starblink.rocketreserver.FetchSubscribeMerchantWebRecommendQuery
import com.starblink.rocketreserver.SearchMerchantWebsQuery
import com.starblink.rocketreserver.type.MerchantWebRecommendReq
import com.starblink.rocketreserver.type.MerchantWebSearchReq
import com.starblink.search.R
import com.starblink.search.result.repository.model.GoodsRecommendTileModel
import com.starblink.search.result.repository.model.GoodsResultEmptyModel
import com.starblink.search.result.ui.fragment.BaseSearchVM
import kotlinx.coroutines.launch

class StoreResultVM : BaseSearchVM() {

    val contentList = arrayListOf<Any>()

    val emptyModel: GoodsResultEmptyModel by lazyOnNone {
        GoodsResultEmptyModel(
            emptyTxt1 = string(com.starblink.basic.style.R.string.default_empty_text1),
            emptyTxt2 = string(R.string.search_store_empty_tip)
        )
    }

    val recTitleModel: GoodsRecommendTileModel by lazyOnNone {
        GoodsRecommendTileModel(
            text = string(com.starblink.basic.style.R.string.you_may_like)
        )
    }

    val contentLD = MutableLiveData<Boolean>()


    var contentSearch = ""
    var searchWordSource: Int? = null

    var pageNo = 1

    var recPageNo = 1

    private var recSizeTag = 0

    fun loadData() {
        viewModelScope.launch {
            val response = SkGqlClient.skQuery(
                SearchMerchantWebsQuery(
                    params = MerchantWebSearchReq(
                        contentSearch = contentSearch,
                        pageNo = pageNo.optional(),
                        pageSize = PAGE_SIZE.optional()
                    )
                )
            )
            val list =
                response?.data?.searchMerchantWebs?.map { it.merchantWebVOF } ?: arrayListOf()

            if (pageNo == 1 && list.isNullOrEmpty()) {
                loadRecStore()
            } else {
                loadSuccess()
                contentList.addAll(list)
                contentLD.postValue(list.isNotEmpty())
            }

        }
    }

    fun loadRecStore() {
        viewModelScope.launch {
            val response = SkGqlClient.skQuery(
                FetchSubscribeMerchantWebRecommendQuery(
                    merchantParams = MerchantWebRecommendReq(
                        pageNo = recPageNo,
                        pageSize = PAGE_SIZE,
                        clientTime = "",
                        sceneCode = 4,
                    )
                )
            )

            val list =
                response?.data?.fetchSubscribeMerchantWebRecommend?.merchantWebList?.mapIndexed { index, it ->
                    SearchStoreRecommendModel(
                        it.merchantWebVOF,
                        index = index + recSizeTag,
                    )
                }
                    ?: arrayListOf()

            recSizeTag += list.size

            if (recPageNo == 1) {
                contentLD.postValue(list.isNotEmpty())
                if (list.isNotEmpty()) {
                    contentList.add(emptyModel)
                    contentList.add(recTitleModel)
                    contentList.addAll(list)
                    loadSuccess()
                } else {
                    loadNoData()
                }
            } else {
                contentList.addAll(list)
                contentLD.postValue(list.isNotEmpty())
                loadSuccess()
            }

        }
    }

    val contentHasResult: Boolean
        get() = contentList.firstOrNull() != emptyModel

    companion object {

        const val PAGE_SIZE = 20

    }


}